package com.dollar.ruyuan_juc.nonLockImpl.immutable.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 如何实现一个不可变类
 */
public final class Location { //1、final 禁止被子类修改
    /**
     * 2、final 修饰成员变量，禁止修改
     * 注意：如果类中存在数组或集合，在提供给外部访问之前需要做防御性复制
     * return Collections.unmodifiableList(new ArrayList<>(this.datas));
     */
    private final double x;
    private final double y;
    private final List<Integer> datas;

    public Location(double x, double y, List<Integer> datas) {
        this.x = x;
        this.y = y;
        this.datas = datas;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public List<Integer> getDatas() {
        return Collections.unmodifiableList(new ArrayList<>(this.datas));
    }
}
